import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 创建一个三维数组
arr3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# 获取数组的形状
depth, rows, cols = arr3d.shape

# 创建一个 3D 图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 绘制每个元素的坐标，并添加标注
for i in range(depth):
    for j in range(rows):
        for k in range(cols):
            # 绘制散点
            ax.scatter(k, j, i, color=plt.cm.viridis(arr3d[i, j, k] / 8))  # 使用颜色映射
            # 添加文本标注，显示数组中的值
            ax.text(k, j, i, f'({i},{j},{k})={arr3d[i,j,k]}', color='black')

# 设置轴标签
ax.set_xlabel('Columns (k)')
ax.set_ylabel('Rows (j)')
ax.set_zlabel('Depth (i)')

# 设置图标题
ax.set_title('3D Array Visualization with Labels')

# 设置轴范围
ax.set_xlim([0, cols-1])
ax.set_ylim([0, rows-1])
ax.set_zlim([0, depth-1])

plt.show()
